var t = require('../@babel/runtime/helpers/objectSpread2.js'),
  e = require('../@babel/runtime/helpers/regeneratorRuntime.js'),
  r = require('../@babel/runtime/helpers/asyncToGenerator.js'),
  i = require('../@babel/runtime/helpers/classCallCheck.js'),
  n = require('../@babel/runtime/helpers/createClass.js'),
  s = require('A9B0993670B67DCFCFD6F1317FB01B27.js');
require('1E1DDD9770B67DCF787BB59075001B27.js');
module.exports = (function () {
  function a() {
    i(this, a),
      (this.URL_MAP = {
        getSets: {
          url: 'user_action_sets/get?version=v1.0&access_token=',
          method: 'GET',
        },
        addSets: {
          url: 'user_action_sets/add?version=v1.0&access_token=',
          method: 'POST',
        },
        addAction: {
          url: 'user_actions/add?version=v1.0&access_token=',
          method: 'POST',
        },
      }),
      (this.ACTION_URL_MAP = {
        REGISTER: 'http://www.pages/index/index',
        COMPLETE_ORDER: 'http://www.qq.com',
      }),
      (this.BASE_URL = 'https://api.weixin.qq.com/marketing/'),
      (this.___token = ''),
      (this.___tryFlag = !0),
      (this.taskList = []),
      (this.waitTimer = null),
      (this.click_id = '');
  }
  var c, o;
  return (
    n(a, [
      {
        key: 'addTask',
        value: function (t, e) {
          try {
            var r = { functionName: t, params: e, lockStatus: 'unplayed' };
            this.taskList.push(r),
              this.taskList.find(function (t) {
                return 'running' === t.lockStatus;
              }) || this.doTask();
          } catch (t) {
            console.error(t);
          }
        },
      },
      {
        key: 'doTask',
        value: function () {
          var t = this;
          try {
            var e = this.taskList;
            if (
              e.length &&
              e.find(function (t) {
                return 'running' === t.lockStatus;
              })
            )
              return (
                clearTimeout(this.waitTimer),
                void (this.waitTimer = setTimeout(function () {
                  t.doTask();
                }, 500))
              );
            var r = e.findIndex(function (t) {
              return 'unplayed' === t.lockStatus;
            });
            if (-1 != r) {
              var i = e[r];
              console.log('但是队列中还有未执行的任务, \ntargetTask: ', i),
                this[i.functionName](i.params),
                (e[r].lockStatus = 'running'),
                (this.taskList = e);
            }
          } catch (t) {
            console.error(t);
          }
        },
      },
      {
        key: 'taskDown',
        value: function () {
          try {
            var t = this.taskList.findIndex(function (t) {
              return 'running' === t.lockStatus;
            });
            -1 != t && ((this.taskList[t].lockStatus = 'down'), this.doTask());
          } catch (t) {
            console.error(t);
          }
        },
      },
      {
        key: 'userActions',
        value:
          ((o = r(
            e().mark(function t(r) {
              var i, n, s;
              return e().wrap(
                function (t) {
                  for (;;)
                    switch ((t.prev = t.next)) {
                      case 0:
                        return (
                          (t.prev = 0),
                          (i = r.key),
                          (n = r.action_param),
                          (s = {
                            user_action_set_id: this.user_action_set_id,
                            actions: [
                              {
                                url: this.ACTION_URL_MAP[i],
                                action_type: i,
                                trace: { click_id: this.click_id },
                                action_param: n,
                              },
                            ],
                          }),
                          (t.next = 5),
                          this.adReq('addAction', s)
                        );
                      case 5:
                        this.taskDown(), (t.next = 11);
                        break;
                      case 8:
                        (t.prev = 8), (t.t0 = t.catch(0)), console.error(t.t0);
                      case 11:
                      case 'end':
                        return t.stop();
                    }
                },
                t,
                this,
                [[0, 8]],
              );
            }),
          )),
          function (t) {
            return o.apply(this, arguments);
          }),
      },
      {
        key: 'adReq',
        value: function (e, r) {
          var i = this;
          try {
            return new Promise(function (n, a) {
              var c = i.___token,
                o = i.URL_MAP,
                u = i.BASE_URL,
                l = i,
                d = o[e],
                _ = d.url,
                h = d.method,
                k = {
                  params: r,
                  url: ''.concat(u).concat(_).concat(c),
                  method: h,
                };
              s.user.transferRequest(t({}, k)).then(function (t) {
                if (0 == t.errcode) n(t.data);
                else if (-1 == t.errcode) a('adReq 异常 服务繁忙', t);
                else {
                  if (t.errmsg.includes('access_token is invalid')) {
                    if (!l.___tryFlag)
                      return void console.error(
                        'time after time access_token is invalid, ___tryFlag: ',
                        ___tryFlag,
                      );
                    (l.___tryFlag = !1),
                      l.init({ query: { gdt_vid: l.click_id }, force: 1 });
                  }
                  a('adReq 异常', t);
                }
              });
            });
          } catch (t) {
            console.error(t);
          }
        },
      },
      {
        key: 'init',
        value:
          ((c = r(
            e().mark(function t(r) {
              var i, n, a, c, o, u, l;
              return e().wrap(
                function (t) {
                  for (;;)
                    switch ((t.prev = t.next)) {
                      case 0:
                        return (
                          (t.prev = 0),
                          (i = r.query),
                          (n = r.force),
                          i && (this.click_id = i.gdt_vid),
                          (t.next = 5),
                          s.user.getToken({ force: n })
                        );
                      case 5:
                        return (
                          (a = t.sent),
                          (c = a.access_token),
                          (this.___token = c),
                          (t.next = 10),
                          this.adReq('getSets')
                        );
                      case 10:
                        if (
                          !(
                            (o = t.sent).list &&
                            o.list.length &&
                            o.list.find(function (t) {
                              return 'WEB' === t.type;
                            })
                          )
                        ) {
                          t.next = 16;
                          break;
                        }
                        (u = o.list.find(function (t) {
                          return 'WEB' === t.type;
                        })),
                          (this.user_action_set_id = u.user_action_set_id),
                          (t.next = 20);
                        break;
                      case 16:
                        return (t.next = 18), this.adReq('addSets');
                      case 18:
                        (l = t.sent),
                          (this.user_action_set_id = l.user_action_set_id);
                      case 20:
                        this.taskDown(), (t.next = 26);
                        break;
                      case 23:
                        (t.prev = 23), (t.t0 = t.catch(0)), console.log(t.t0);
                      case 26:
                      case 'end':
                        return t.stop();
                    }
                },
                t,
                this,
                [[0, 23]],
              );
            }),
          )),
          function (t) {
            return c.apply(this, arguments);
          }),
      },
      {
        key: 'judgeIsAdIn',
        value: function () {
          try {
            return !!this.click_id;
          } catch (t) {
            console.log(t);
          }
        },
      },
    ]),
    a
  );
})();
